<?php

namespace Biz\model\theParagraph;


class Collocation extends \Doraemon\pockets\datebase\BaseDbModel
{

    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_the_paragraph';

    const _DATABASE_ = 'the_paragraph';

    const _TABLE_NAME = 'collocation';

    // 状态为正常
    const STATUS_NORMAL = 1;
    // 状态为隐藏
    const STATUS_HIDDEN = 2;
    // 状态为被删除
    const STATUS_DELETE = 9;
    const DEFAULE_DATETIME = '0000-00-00 00:00:00';

    const STYLE_ONE = 1;

    const STYLE_TWO = 2;

    const SUB_SCRIPT_NONE = 0;
    const SUB_SCRIPT_NEW = 1;
    const SUB_SCRIPT_HOT = 2;

    const SUB_SCRIPT_LIST=[
        self::SUB_SCRIPT_NONE => '无',
        self::SUB_SCRIPT_NEW => '上新',
        self::SUB_SCRIPT_HOT => '热门'
    ];


    public static function getCollocationListByRoomId($roomId, $status = [self::STATUS_NORMAL])
    {
        return self::select("*", [
            'room_id' => $roomId,
            'status' => $status,
            'ORDER' => [
                'sort',
                'id'
            ]
        ]);
    }

    public static function getValidCollocationIdByRoomId($roomId)
    {
        $date = date('Y-m-d H:i:s');
        return self::get("id", [
            'room_id' => $roomId,
            'status' => self::STATUS_NORMAL,
            'OR #startTime' => [
                'start_time[<=]' => $date,
                'start_time' => self::DEFAULE_DATETIME
            ],
            'OR #endTime' => [
                'end_time[>=]' => $date,
                'end_time' => self::DEFAULE_DATETIME
            ],
            'ORDER' => [
                'sort',
                'id'
            ]
        ]);
    }

    public static function getValidCollocationByRoomIds($roomIds)
    {
        $date = date('Y-m-d H:i:s');
        return self::select("*", [
            'room_id' => $roomIds,
            'status' => self::STATUS_NORMAL,
            'OR #startTime' => [
                'start_time[<=]' => $date,
                'start_time' => self::DEFAULE_DATETIME
            ],
            'OR #endTime' => [
                'end_time[>=]' => $date,
                'end_time' => self::DEFAULE_DATETIME
            ],
            'ORDER' => [
                'sort',
                'id'
            ]
        ]);
    }

    public static function getCollocationByIds($ids)
    {
        return self::select("*", [
            'id' => $ids,
            'status' => self::STATUS_NORMAL
        ]);
    }

    public static function getCollocationById($id)
    {
        return self::get("*", ['id' => $id]);
    }

    public static function saveCollocationInfo($collocationInfo, $id = 0)
    {
        if ($id) {
            $collocationInfo['edittime'] = date("Y-m-d H:i:s");
            $rs = self::update($collocationInfo, ["id" => $id])->rowcount();
//            var_dump(self::log());
//            var_dump($rs);
            if ($rs) {
                return $id;
            } else {
                return false;
            }
        } else {
            $collocationInfo['status'] = self::STATUS_HIDDEN;
            $collocationInfo['addtime'] = date("Y-m-d H:i:s");
            if (self::insert($collocationInfo)->rowcount()) {
                return self::id();
            } else {
                return false;
            }
        }
    }

    public static function changeCollocationStatusById($id, $status)
    {
        if (self::update(['status' => $status], ["id" => $id])->rowcount()) {
            return $id;
        }
    }

    public static function saveCollocationSort($sortInfo)
    {
        foreach ($sortInfo as $collocationId => $sort) {
            self::update(['sort' => $sort], ['id' => $collocationId]);
        }
    }

}

